/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bankingsystem;

import java.util.Hashtable;
import java.util.Scanner;

/**
 * This class corresponds to the Online Banking facility.
 * User has to input login details first. If the user is able to access, he can
 * view the account balance, current interest of his Fixed Account and his loan status.
 * @author Sumedha
 */
public class OnlineBanking {

    private Bank bank;
    private BankInterface bankIn;
    private Hashtable<String, Customer> customers;      //To store customer objects
    private Hashtable<String, FixedAccount> fixedAccounts;  //To store fixed account objects
    private Hashtable<String, CurrentAccount> currentAccounts;  //To store current account objects
    private Hashtable<String, SavingsAccount> savingsAccounts;  //To store savings account ojects
    private Hashtable<String, Loan> loans;  //To store loan objects
    private Scanner in; //To read the input in the console
    private int choice; //To read the input in the console
    private int value;  //To read the input in the console

    /**
     * This creates a OnlineBanking object with the given parameters
     * @param bank The bank object
     * @param bi The bank Interface object
     */
    public OnlineBanking(Bank bank, BankInterface bi) {

        this.bank = bank;
        this.bankIn = bi;
        this.customers = bank.getCustomers();
        this.fixedAccounts = bank.getFixedAccounts();
        this.currentAccounts = bank.getCurrentAccounts();
        this.savingsAccounts = bank.getSavingsAccounts();
        this.loans = bank.getLoans();
        System.out.println("******* Welcome To CodeX Bank *******\n");
        in = new Scanner(System.in);
        login();

    }

    /**
     * This method provides the login for customers. Customers given the ability
     * to enter his customer id and password in the console and if the login is
     * successful he will be directed to the next stage.
     */
    private void login() {

        //Getting the customer ID
        System.out.print("Please Enter your CustomerID: ");
        String customerId = in.nextLine();

        //Getting the Password
        System.out.print("Please Enter your Password: ");
        String password = in.nextLine();

        //Getting the corresponding customer object to the given customer ID
        Customer customer = (Customer) customers.get(customerId);

        //Checks if the cutomer exists
        if (customer != null) {

            //If the customer exist check password validity
            if (customer.checkPassword(password)) {

                System.out.println("\n------ Login successfull ------");
                //If the customer successfully logged in he will see the next options
                display();
                //According to the choce the actions will be carried out
                doAction(customer);

            }//If the password is incorrect
            else {

                System.out.println("Invalid Password.Please Try Again. ");
                login();

            }

        } //If the user does not exist
        else {

            System.out.println("Invalid customer ID.Please Try again.");
            login();

        }
    }

    /**
     * This method displays the preferences that the customer has
     *
     */
    private void display() {

        System.out.println("\nPlease Select your Prference\n");
        System.out.println("1: Check account balanace \n2: Check loan status\n3: Go back to main menu");
        System.out.print("\nPlease Enter your choice: ");

    }

    /**
     * this method is used to get the choice made by the customer using a scanner object
     * @return The value of the selected preference;
     * 1: Check account balance 2: Check loan status 3: Go back
     */
    private int observeChoice() {

        try {
            value = in.nextInt(); //capturing the user's next keystroke.
        } catch (Exception e) {
            System.out.println("Enter an integer only.");
            bankIn.init();
            System.exit(0);
        }
        return value;

    }

    /**
     * This method performs the actions depending on the choice of the user
     * @param choice  choice of the customer
     * @param customer    customer object of the user
     */
    private void doAction(Customer customer) {

        //He is given to choose his preference
        choice = observeChoice();

        //If the choice is to Check account balance
        if (choice == 1) {

            //Customer is given to chose the account type to see the acount balance
            System.out.println("Select accunt type: \n1: Current Account\n2: Savings Account\n3: Fixed Account");
            System.out.print("\nEnter your preference: ");
            int accountType = observeChoice();

            //If it is a current account
            if (accountType == 1) {

                try {
                    //Get the current account. each customer object has his account number
                    CurrentAccount ca = currentAccounts.get(customer.getCurrentAccountNo());

                    //If the customer has a current account
                    if (ca != null) {
                        System.out.println("----- Current Account Number: " + ca.accountNo + " -----");
                        System.out.println("Account Balance: " + ca.balance);
                        /**
                         * After completion of the task the user is given the option to whether end the
                         * current session or continue with another action
                         */
                        continueTransActions(customer);

                    }//If the customer does not have a current account
                    else {

                        System.out.println("You Don't have a Current account. Please try another option.");
                        //Displays main menu and directs back to the main menu
                        display();
                        doAction(customer);

                    }

                } //If the customer does not have a current account
                catch (Exception e) {

                    System.out.println("You Don't have a Current account. Please try another option.");
                    //Displays main menu and directs back to the main menu
                    display();
                    doAction(customer);

                }
            }//If it is a Savings account
            else if (accountType == 2) {

                try {

                    //Get the Savings account. each customer object has his account number
                    SavingsAccount sa = savingsAccounts.get(customer.getSavingsAccountNo());

                    //If the customer has a Savings account
                    if (sa != null) {

                        System.out.println("----- Savings Account Number: " + sa.accountNo + " -----");
                        System.out.println("Account Balance: " + sa.balance);
                        /**
                         * After completion of the task the user is given the option to whether end the
                         * current session or continue with another action
                         */
                        continueTransActions(customer);

                    } //If the customer does not have a savings account
                    else {

                        System.out.println("You Don't have a Savings account. Please try another option.");
                        //Displays main menu and directs back to the main menu
                        display();
                        doAction(customer);
                    }

                }//If the customer does not have a savings account
                catch (Exception e) {

                    System.out.println("You Don't have a Savings account. Please try another option.");
                    //Displays main menu and directs back to the main menu
                    display();
                    doAction(customer);

                }
            }//Fixed Deposit
            else if (accountType == 3) {

                try {

                    //Get the Fixed account. each customer object has his account number
                    FixedAccount fa = fixedAccounts.get(customer.getFixedAccountNo());

                    //If the customer has a Fixed account
                    if (fa != null) {

                        System.out.println("----- Fixed Account Number: " + fa.accountNo + " -----");
                        System.out.println("Current Interest: " + fa.getInterestRate());
                        System.out.println("Remaining Duration: " + fa.getDuration());
                        /**
                         * After completion of the task the user is given the option to whether end the
                         * current session or continue with another action
                         */
                        continueTransActions(customer);

                    }//If the customer does not have a Fixedaccount
                    else {

                        System.out.println("You Don't have a Fixed account. Please try another option.");
                        //Displays main menu and directs back to the main menu
                        display();
                        doAction(customer);

                    }
                }//If the customer does not have a Fixedaccount
                catch (Exception e) {

                    System.out.println("You Don't have a Fixed account. Please try another option.");
                    //Displays main menu and directs back to the main menu
                    display();
                    doAction(customer);

                }

            }//If the input is invalid
            else {

                System.out.println("Invalid input");
                //Displays main menu and directs back to the main menu
                display();
                doAction(customer);

            }

        } //If the customer need to check his loan status
        else if (choice == 2) {

            try {
                //Getting the loan object corrsponding to the customer
                Loan loan = (Loan) loans.get(customer.getLoanNo());

                //Checks wheather the customer has a loan
                if (loan != null) {

                    //Display loan details
                    System.out.println("------Loan No: " + loan.getLoanNo() + " ------");
                    System.out.println("Amount remaining to be paid: " + loan.getAmount());
                    System.out.println("Remaining duration to settle the loan: " + loan.getRemainingDuration() + " months");
                    /**
                     * After completion of the task the user is given the option to whether end the 
                     * current session or continue with another action
                     */
                    continueTransActions(customer);

                } //If the customer does not have a loan
                else {

                    System.out.println("You don't have a loan.Please try another option.");
                    //Displays main menu and directs back to the main menu
                    display();
                    doAction(customer);

                }
            } catch (Exception e) {

                System.out.println("You Don't have a Loan. Please try another option.");
                //Displays main menu and directs back to the main menu
                display();
                doAction(customer);

            }

        } else if (choice == 3) {
            System.out.println("Thank You for using CodeX Online Banking System.Have a nice day....");
            bankIn.init();

        }//If the input choice is invalid
        else {

            System.out.println("Invalid input");
            //Displays main menu and directs back to the main menu
            display();
            doAction(customer);

        }

    }

    /**
     * This method is used for continuation of the actions. That is after customer has
     * completed a task he will be given the chance to do another action.
     * @param customer customer object which has been logged in
     */
    private void continueTransActions(Customer customer) {

        System.out.println("\nDo want to continue with another action");
        System.out.print("1: Yes\n2: No\n\nEnter Your Preference: ");
        int choise = observeChoice();

        if (choise == 1) {
            //Displays main menu and directs back to the main menu
            display();
            doAction(customer);

        } else if (choise == 2) {
            //Customer is directed to the main menu of the banking system
            System.out.println("Thank You for using CodeX Online Banking System.Have a nice day....");
            bankIn.init();

        } else {
            //If the user enters an invalid input
            System.out.println("Invalid input. Please try again.");
            continueTransActions(customer);

        }
    }
}
